<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span> {{ status }}</span>
      </div>
      <div>
        {{ "订单号:" + orderSn }}
      </div>
      <div>
        {{ "应付金额:" + payAmount + "元" }}
      </div>
      <div>
        <el-button type="primary" v-if="status!='支付成功'" @click="pay()">支付</el-button>
        <el-button type="primary" v-if="status=='支付成功'" @click="goProductList()">再逛逛</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {getRedisOrder, paymentOrder} from "@/api/settleAccounts/settleAccounts";

export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    //这里存放数据"

    return {
      selectOrderToken: null,
      status: null,
      orderSn: null,
      payAmount: null,
      syncRedisCount: 0
    };
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    startChecking() {
      getRedisOrder(this.selectOrderToken).then(res => {
        if (res.data.status == 1) {
          this.status = '已支付,等待发货中....';
          this.orderSn = res.data.orderSn
          this.payAmount = res.data.payAmount
          clearInterval(this.intervalId);
          return;
        }
      });
      this.status = '正在下单...';
      this.intervalId = setInterval(() => {
        this.checkStatus();
      }, 800); // 每两秒检查一次
    },
    checkStatus() {
      // 调用API
      getRedisOrder(this.selectOrderToken).then(res => {
        console.log('查询结果:', res);
        if (res.code === 200) {
          // 结果满足条件，停止循环
          if (this.syncRedisCount == 1) {
            return;
          }
          clearInterval(this.intervalId);
          this.handleResult(res);
          this.syncRedisCount = this.syncRedisCount + 1
        }
      });
    },
    handleResult(data) {
      // 处理结果数据
      console.log('最终结果:', data.data);
      this.orderSn = data.data.orderSn
      this.payAmount = data.data.payAmount
      if (data.data.status == 0) {
        this.status = '下单成功';
        paymentOrder(this.selectOrderToken).then(res => {

        })
      }
      if (data.data.status == 1) {
        this.status = '支付成功';
      }
      if (data.data.status == 5) {
        this.status = '下单失败,请联系商家';
      }
      // 在这里添加你需要的操作
      this.$message.success('下单成功,请在15分钟内支付')
    },
    pay() {
      window.open("http://localhost:9109/alipay/pay?subject=我的订单&traceNo=" + this.orderSn + "&totalAmount=" + this.payAmount)
    },
    goProductList() {
      this.$router.push({name: 'Product'});
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.selectOrderToken = localStorage.getItem("selectOrderToken")
    this.payAmount = localStorage.getItem("payAmount")
    this.startChecking();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, // 生命周期 - 更新之后",
  beforeDestroy() {
  }, // 生命周期 - 销毁之前",
  destroyed() {
  }, // 生命周期 - 销毁完成",
  activated() {
  } // 如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

</style>
